package m.html.form;

import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class FormJsTest {
    private final List<String> testList;
    private final List<String> trueJsList;
    private String otherJs;
    private FormJsTest(String test,String testJs){
        this.testList=new ArrayList<>();
        this.testList.add(test);
        this.trueJsList=new ArrayList<>();
        this.trueJsList.add(testJs);
    }
    public FormJsTest elseTest(String test,String testJs){
        this.testList.add(test);
        this.trueJsList.add(testJs);
        return this;
    }
    public FormJsTest elseJs(String js){
        this.otherJs=js;
        return this;
    }

    protected String toJsString(){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<this.testList.size();i++){
            if(i!=0) sb.append(" else ");
            sb.append("if(").append(this.testList.get(i)).append("){").append(this.trueJsList.get(i)).append("}");
        }
        if(StringUtil.notSpace(this.otherJs)){
            sb.append(" else {").append(this.otherJs).append("}");
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return toJsString();
    }

    /**
     * 判断语句
     */
    public static FormJsTest test(String test,FormJsTest testJs){
        return new FormJsTest(test,testJs.toJsString());
    }
    /**
     * 判断语句
     */
    public static FormJsTest test(String test,String testJs){
        return new FormJsTest(test,testJs);
    }


    /**
     * 判断，false弹出消息 返回false
     */
    public static FormJsTest falseRemind(String test,String remind){
        return new FormJsTest(test,"").elseJs("this.remind().error(\""+remind+"\");return false;");
    }
}
